package com.jml.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jml.bean.Depart;
import com.jml.fallback.DepartControllerFallback;
import com.jml.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供者Controller，对外提供接口
@RequestMapping("/provider/depart")
@RestController
public class DepartController {
    @Autowired
    private DepartService departServiceImpl;

    //新增
    @PostMapping("/save")
    public boolean saveHandle(@RequestBody Depart depart) {
        return departServiceImpl.saveDepart(depart);
    }

    //删除
    @DeleteMapping("/del/{id}")
    public boolean deleteHandle(@PathVariable("id") int id) {
        return departServiceImpl.removeDepartById(id);
    }

    //修改
    @PutMapping("/update")
    public boolean updateHandle(@RequestBody Depart depart) {
        return departServiceImpl.modifyDepart(depart);
    }


    /**
     * 熔断慢调用
     * @param id
     * @return
     */
    @GetMapping("/slow/get/{id}")
    @SentinelResource(value = "getSlowDepartById", fallback = "getHandlerFallback")
    public Depart getSlowHandle(@PathVariable("id") int id) {
        return departServiceImpl.getDepartById(id);
    }

    //流控注解
    @SentinelResource(value = "qpsFlowRule",blockHandler = "getHandleBlock")
    @GetMapping("/flow/get/{id}")
    public Depart getFLowHandle(@PathVariable("id") int id) {
        return null;
    }
    //编写流控兜底的方法：注意流控的降级方法，需要多加入一个参数，否则这个方法不会被调用
    //正常情况下：降级方法和被降级的方法参数及返回值类型都是要一样的
    public Depart getHandleBlock(int id, BlockException ex) {
        Depart depart = new Depart(1,"");
        depart.setName("no any more~");
        return depart;
    }

    //根据id查询
    // 方法上的降级，SentinelResource把这个方法声明为一个资源，
    // value是给当前方法指定一个资源名臣，
    // fallback是指降级的方法
    @GetMapping("/get/{id}")
    @SentinelResource(value = "getDepartById", fallback = "getHandlerFallback"
    /*设置流控的兜底方法 , blockHandler = "" ,blockHandlerClass = Object.class*/)
    public Depart getHandle(@PathVariable("id") int id) {
        return departServiceImpl.getDepartById(id);
    }

    public Depart getHandlerFallback(@PathVariable("id") int id) {
        if(id==1){
            throw new RuntimeException("降级抛异常，验证调用方feign的降级");
        }
        System.out.println("执行getDepartById()的服务降级处理方法");
        Depart depart = new Depart(id,"降级处理方法");
        return depart;
    }

    //查询列表   降级方法写在类中
    @GetMapping("/list")
    @SentinelResource(value = "getListHandleFall", fallback = "listAllDeparts",fallbackClass = DepartControllerFallback.class)
    public List<Depart> listHandle() {
        int i=0;
        int a = 1/i;
        return departServiceImpl.listAllDeparts();
    }

    /**
     * 链路限流
     * @return
     */
    @GetMapping("/lian")
    public List<Depart> lian() {
        return departServiceImpl.lianlu();
    }
    @GetMapping("/lu")
    public List<Depart> lu() {
        return departServiceImpl.lianlu();
    }

}
